package com.github.wxpay.sdk.api;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.api.entity.RedPackEntity;
import com.github.wxpay.sdk.config.WXPayConfig;
import com.github.wxpay.sdk.exception.WXPayException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tyf on 2018/11/21
 */
public class WxRedPackApi {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private static final String SUCCESS = "SUCCESS";

    private WXPayConfig config = null;

    public WxRedPackApi(WXPayConfig config) {
        this.config = config;
    }

    /**
     * 发送现金红包
     *
     * @param redPackEntity
     * @throws Exception
     */
    public void sendRedPack(RedPackEntity redPackEntity) throws Exception {
        WXPay wxpay = new WXPay(config);
        Map<String, String> redPackMap = createRedPackMap(redPackEntity);
        Map<String, String> resp = wxpay.sendRedPack(redPackMap);
        String returnCode = resp.get("result_code");
        if (!StringUtils.equals(SUCCESS, returnCode)) {
            //如果不为success,则直接抛出异常
            log.error(resp.toString());
            throw new WXPayException(resp.get("err_code"), resp.get("err_code_des"));
        }
    }

    private Map<String, String> createRedPackMap(RedPackEntity redPackEntity) {
        Map<String, String> data = new HashMap<>();
        if (StringUtils.isNoneBlank(redPackEntity.getMchBillno())) {
            data.put("mch_billno", redPackEntity.getMchBillno());
        }
        if (StringUtils.isNoneBlank(redPackEntity.getSendName())) {
            data.put("send_name", redPackEntity.getSendName());
        }
        if (StringUtils.isNoneBlank(redPackEntity.getReOpenid())) {
            data.put("re_openid", redPackEntity.getReOpenid());
        }
        if (redPackEntity.getTotalAmount() != null) {
            //需要乘以100,入参为元,这里改为分
            data.put("total_amount", String.valueOf(
                    redPackEntity.getTotalAmount().intValue()));
        }
        if (redPackEntity.getTotalNum() != null) {
            data.put("total_num", String.valueOf(redPackEntity.getTotalNum()));
        }
        if (StringUtils.isNoneBlank(redPackEntity.getWishing())) {
            data.put("wishing", redPackEntity.getWishing());
        }
        if (StringUtils.isNoneBlank(redPackEntity.getClientIp())) {
            data.put("client_ip", redPackEntity.getClientIp());
        }
        if (StringUtils.isNoneBlank(redPackEntity.getActName())) {
            data.put("act_name", redPackEntity.getActName());
        }
        if (StringUtils.isNoneBlank(redPackEntity.getRemark())) {
            data.put("remark", redPackEntity.getRemark());
        }
        if (StringUtils.isNoneBlank(redPackEntity.getSceneId())) {
            data.put("scene_id", redPackEntity.getSceneId());
        }
        return data;
    }


    /**
     * 查询红包状态,0标示需要重复查询,1标示成功,2标示失败
     *
     * @param wxMchId
     * @return
     * @throws Exception
     */
    public int selectRedPackStatus(String wxMchId) throws Exception {

        WXPay wxpay = new WXPay(config);
        Map<String, String> data = new HashMap<>();
        data.put("mch_billno", wxMchId);
        data.put("bill_type", "MCHT");
        Map<String, String> resp = wxpay.selectRedPack(data);
        String returnCode = resp.get("result_code");
        if (!StringUtils.equals(SUCCESS, returnCode)) {
            //如果不为success,则直接抛出异常
            log.error(resp.toString());
            throw new WXPayException(resp.get("err_code"), resp.get("err_code_des"));
        }
        return getStatus(resp);
    }


    private int getStatus(Map<String, String> resp) {
        final String status = resp.get("status");
        if (StringUtils.equals(status, "SENDING")) {
            return 0;
        }
        if (StringUtils.equals(status, "SENT")) {
            return 0;
        }
        if (StringUtils.equals(status, "FAILED")) {
            return 2;
        }
        if (StringUtils.equals(status, "RECEIVED")) {
            return 1;
        }
        if (StringUtils.equals(status, "RFUND_ING")) {
            return 0;
        }
        if (StringUtils.equals(status, "REFUND")) {
            return 2;
        }
        return 0;
    }
}
